# This is test4.py
class Node:
    def __init__(self, _data=None, _next=None):
        self.data = _data
        self.next = _next

    def __str__(self):
        return str(self.data)


class LinkedList:
    def __init__(self):
        """
        初始化链表，创建一个空的头节点
        """
        self.head = Node()

    def __str__(self):
        """
        返回链表的字符串表示形式
        """
        ret = []
        temp = self.head.next  # 从第一个实际节点开始
        while temp:
            ret.append(str(temp))
            temp = temp.next
        return '->'.join(ret)

    def push(self, _data):
        """
        在链表尾部添加一个新节点
        """
        node = Node(_data)
        if self.head.next is None:
            self.head.next = node
        else:
            curr = self.head.next
            while curr.next:
                curr = curr.next
            curr.next = node

    def pop(self):
        """
        移除并返回链表的最后一个节点
        """
        if self.isEmpty():
            raise IndexError('pop from empty LinkedList')
        prev = self.head
        curr = prev.next
        while curr.next:
            prev = curr
            curr = curr.next
        prev.next = None
        return curr.data

    def __iter__(self):
        """
        迭代器方法，允许对链表进行迭代
        """
        curr = self.head.next  # 从第一个实际节点开始
        while curr:
            yield curr.data  # 生成当前节点的数据
            curr = curr.next  # 移动到下一个节点

    def isEmpty(self):
        """
        检查链表是否为空
        """
        return self.head.next is None


lst = LinkedList()
lst.push(1)
lst.push(1)
lst.push(3)
lst.push(3)
lst.push(5)
lst.push(5)
lst.push(7)

print(lst)
